import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    component: () => import('../views/home/index'),
    children: [
      {
        path: "/home",
        redirect: "/home/shou"
      },
      {
        path: '/home/shou',
        component: () => import('../views/home/shou')
      },
      {
        path: '/home/classify',
        component: () => import('../views/home/classify')

      },
      {
        path: '/home/cart',
        component: () => import('../views/home/cart'),
        meta: {
          reqireLogin: true
        },
      },
      {
        path: '/home/mine',
        component: () => import('../views/home/Mine')

      },
      {
        path: '/home/classify',
        component: () => import('../views/home/classify')

      },
    ]
  },
  {
    path: '/detail/:id',
    props: true,
    component: () => import('../views/detail/index'),
  },
  {
    path: '/login',
    component: () => import('../views/login/index.vue'),
  },
  {
    path: '/info',
    component: () => import('../views/info/index'),
    meta: {
      reqireLogin: true
    },
  },
  {
    path: '/changeInfo',
    component: () => import('../views/info/changeUserInfo'),
    meta: {
      reqireLogin: true
    },

  },
  {
    path: '/address',
    component: () => import('../views/address/index'),
    meta: {
      reqireLogin: true
    },
  },
  {
    path: '/addaddress',
    component: () => import('../views/address/addAddress'),
    meta: {
      reqireLogin: true
    },
  },
  {
    path: '/updateaddress',
    component: () => import('../views/address/updateAddress'),
    meta: {
      reqireLogin: true
    },
  }


];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.reqireLogin) {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
})

export default router;
